/**
 * 微服务认证授权模块
 * @author lms 2022.08.11
 */
package ms.core.gateway.service;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ms.core.common.ApiResult;
import ms.core.common.RequestBody;
import ms.core.common.consts.SysErr;
import ms.core.gateway.utils.LWGUtils;
import ms.core.gateway.utils.LWGUtils.LWGClient;
import ms.core.tool.StrTool;

public class LWGService {

	/**
	 * 接口：<div style="color:red"><b> lwgLogin </b></div><p>
	 * 功能：微服务注册获取授权码<p>
	 * 说明: sign=md5(cientId+"~"+secretKey+"-"+serviceUrl)
	 * 
	 * @param body={"clientId":"微服务ClientId","t":unix时间戳,"serviceUrl":"微服务地址","sign":"签名"}<p>
	 * @return ApiResult对象
	 */	
	public ApiResult lwgLogin(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		RequestBody rb = RequestBody.getInstance(body);
		String clientId = rb.getStrValue("clientId", null);
		long t = rb.getLongValue("t", 0);
		String serviceUrl = rb.getStrValue("serviceUrl", null);
		String sign = rb.getStrValue("sign", null);
		if (StrTool.isBlank(clientId) || StrTool.isBlank(serviceUrl) || StrTool.isBlank(sign))
			return new ApiResult(false, SysErr.SYS_PARAM_ERR);

		LWGClient client = LWGUtils.getLWGClient(clientId);
		if (client==null)
			return new ApiResult(false, SysErr.SYS_CLIENT_NOT_FOUND);
		
		String signature = client.getSignature(t);
		if (!sign.equals(signature))
			return new ApiResult(false, SysErr.SYS_SIGNATURE_ERR, "签名错误");

		LWGUtils.registerService(clientId, serviceUrl);
		return new ApiResult(true);
	}

	/**
	 * 接口：<div style="color:red"><b> lwgLogout </b></div><p>
	 * 功能：微服务注销<p>
	 * 说明: sign=md5(cientId+"~"+secretKey+"-"+serviceUrl)
	 * 
	 * @param body={"clientId":"微服务ClientId","t":unix时间戳,"serviceUrl":"微服务地址","sign":"签名"}<p>
	 * @return ApiResult对象
	 */	
	public ApiResult lwgLogout(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		RequestBody rb = RequestBody.getInstance(body);
		String clientId = rb.getStrValue("clientId", null);
		String serviceUrl = rb.getStrValue("serviceUrl", null);
		long t = rb.getLongValue("t", 0);
		String sign = rb.getStrValue("sign", null);
		if (StrTool.isBlank(clientId) || StrTool.isBlank(serviceUrl) || StrTool.isBlank(sign))
			return new ApiResult(false, SysErr.SYS_PARAM_ERR);

		LWGClient client = LWGUtils.getLWGClient(clientId);
		if (client==null)
			return new ApiResult(false, SysErr.SYS_CLIENT_NOT_FOUND);
		
		String signature = client.getSignature(t);
		if (!sign.equals(signature))
			return new ApiResult(false, SysErr.SYS_SIGNATURE_ERR, "签名错误");
		
		LWGUtils.unregisterService(clientId, serviceUrl);
		return new ApiResult(true);
	}

	/**
	 * 接口：<div style="color:red"><b> qryAuth </b></div><p>
	 * 功能：查询注册的微服务<p>
	 * 
	 * @param body
	 * @return ApiResult对象
	 */	
	public ApiResult queryAuth(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		return new ApiResult(true).setData(LWGUtils.getLWGClientList());
	}	
}
